package com.lishe.lishe.lishe.module.coupon.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.lishe.lishe.R;
import com.lishe.base.common.base.BaseFragment;
import com.lishe.lishe.lishe.common.Globals;
import com.lishe.lishe.lishe.module.coupon.adapter.CouponListAdapter;
import com.lishe.lishe.lishe.module.coupon.bean.CouponListBean;
import com.lishe.lishe.lishe.module.coupon.http.CouponHttp;
import com.lishe.lishe.lishe.utils.LoadingUtils;
import com.lishe.lishe.lishe.utils.ToastUtil;
import com.lishe.lishe.retrofit.DataResponseListener;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Bin on 2018/6/28.
 */

public class CouponFragment extends BaseFragment implements View.OnClickListener {

    private int mPageNum = 1;
    private int mCoupanType = 0;
    private int mCouponUseStatus; //优惠券领用状态（1：未领取，2：未使用，3：已使用，4：已过期）
    private RecyclerView mRecyclerView;
    private SmartRefreshLayout mRefreshLayout;
    private View mErrorLayout;
    private View mEmptyLayout;
    private CouponListAdapter mAdapter;
    private List<CouponListBean.DataBean.ListBean> mList = new ArrayList<>();

    public static CouponFragment newInstance(int type) {
        CouponFragment fragment = new CouponFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mCoupanType = getArguments().getInt("type");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_coupon, null);
        switch (mCoupanType) {
            case 0:
                mCouponUseStatus = 2;
                mAdapter = new CouponListAdapter(getActivity(), CouponListAdapter.COUPON_UNUSE, mList);
                break;
            case 1:
                mCouponUseStatus = 3;
                mAdapter = new CouponListAdapter(getActivity(), CouponListAdapter.COUPON_USED, mList);
                break;
            case 2:
                mCouponUseStatus = 4;
                mAdapter = new CouponListAdapter(getActivity(), CouponListAdapter.COUPON_OVERDUE, mList);
                break;
        }
        initView(v);
        getData(Globals.TYPE_Edit);
        return v;
    }

    private void initView(View v) {
        mRecyclerView = v.findViewById(R.id.recyclerView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecyclerView.setAdapter(mAdapter);
        mRefreshLayout = v.findViewById(R.id.refreshlayout);
        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                mPageNum = 1;
                getData(Globals.TYPE_Refresh);
            }
        });
        mRefreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                mPageNum++;
                getData(Globals.TYPE_LoadMore);
            }
        });

        mErrorLayout = v.findViewById(R.id.error_layout);
        v.findViewById(R.id.error_retry).setOnClickListener(this);
        mEmptyLayout = v.findViewById(R.id.empty_layout);
    }

    public void getData(final int type) {
        /*
        couponUseStatus	否	int	2	优惠券领用状态（1：未领取，2：未使用，3：已使用，4：已过期）
        isOverdue	否	int	0	是否过期(0：不判断，1：过期，2未过期)
        pageNum	否	int	1	页码
        pageSize	否	int	10	每页显示条数
        token	是	string		token
        */
        if (type == Globals.TYPE_Edit) {
            mPageNum = 1;
            LoadingUtils.createLoadingDialog(getActivity(), "加载中...", true).show();
        }
        //toast(mCouponUseStatus + "..." + mPageNum + "..." + Globals.getToken());
        CouponHttp.getCouponList(mCouponUseStatus, mPageNum, Globals.getToken(), new DataResponseListener<CouponListBean>() {
            @Override
            public void onSuccess(CouponListBean bean) {
                if (type == Globals.TYPE_Edit || type == Globals.TYPE_Refresh) {
                    mList.clear();
                }
                mList.addAll(bean.getData().getList());
                mAdapter.notifyDataSetChanged();
                if (mList.size() == 0) {
                    mRefreshLayout.setVisibility(View.GONE);
                    mEmptyLayout.setVisibility(View.VISIBLE);
                } else {
                    mRefreshLayout.setVisibility(View.VISIBLE);
                }
                if (type == Globals.TYPE_Edit) {
                    LoadingUtils.closeDialog();
                    mRecyclerView.scrollToPosition(0);
                } else if (type == Globals.TYPE_Refresh) {
                    mRefreshLayout.finishRefresh();
                    mRecyclerView.scrollToPosition(0);
                } else if (type == Globals.TYPE_LoadMore) {
                    mRefreshLayout.finishLoadmore();
                }
            }

            @Override
            public void onFail(String failMessage) {
                if (type == Globals.TYPE_Edit) {
                    LoadingUtils.closeDialog();
                } else if (type == Globals.TYPE_Refresh) {
                    mRefreshLayout.finishRefresh();
                } else if (type == Globals.TYPE_LoadMore) {
                    mRefreshLayout.finishLoadmore();
                }
                ToastUtil.showToast(getActivity(), failMessage);
            }

            @Override
            public void onError(Throwable e) {
                if (type == Globals.TYPE_Edit) {
                    LoadingUtils.closeDialog();
                    mRefreshLayout.setVisibility(View.GONE);
                    mErrorLayout.setVisibility(View.VISIBLE);
                } else if (type == Globals.TYPE_Refresh) {
                    mRefreshLayout.finishRefresh();
                    mRefreshLayout.setVisibility(View.GONE);
                    mErrorLayout.setVisibility(View.VISIBLE);
                } else if (type == Globals.TYPE_LoadMore) {
                    mPageNum--;
                    mRefreshLayout.finishLoadmore();
                    ToastUtil.showToast(getActivity(), R.string.network_or_server_exception);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.error_retry:
                mPageNum = 1;
                getData(Globals.TYPE_Edit);
                break;
        }
    }
}
